/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Monster
 */
public class MH_Menu extends Canvas implements CommandListener {

    private Game app;
    private Command _ok;
    private int _width;
    private int _height;
    Image image1 = null;
    Image image2 = null;
    Image exit=null;
    Image newgame=null;
    Image[] item = new Image[2];
    Animation _item_newgame;
    Animation _item_exit;
    private int _itemx;
    
    private int index=0;
    public MH_Menu(Game app) {
        super();
        this.app = app;
        this._ok = new Command("Ok", Command.OK, 1);
        this.addCommand(_ok);
        this.setCommandListener(this);

        try {
            image1 = Image.createImage("//res//menu//Title.png");
            image2 = Image.createImage("//res//menu//bg.png");
            newgame=Image.createImage("//res//menu//newgame.png");
            exit=Image.createImage("//res//menu//exit.png");
            item[0] = Image.createImage("//res//menu//newgame1.png");
            item[1] = Image.createImage("//res//menu//newgame2.png");
            
            _item_newgame=new Animation(item,item.length);
            
            item=new Image[2];
            item[0]=Image.createImage("//res//menu//exit1.png");
            item[1]=Image.createImage("//res//menu//exit2.png");
                       
             _item_exit=new Animation(item,item.length);
             
            _itemx=item[0].getWidth();
            this._height=getHeight();
            this._width=getWidth();

            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
    }

    protected void keyReleased(int keyCode) {
        super.keyReleased(keyCode);
        int result=getGameAction(keyCode);
        if(result==DOWN || result==UP)
        {
            index=(index+1)%2;
            System.out.println(index);
            repaint();
        }
    }

    
    protected void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(image2, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(image1, 0, 0, Graphics.TOP | Graphics.LEFT);
        int xx=(this._width-this._itemx)/2;
        if(index==0)
        {
            g.drawImage(this._item_newgame.GetFrame(),xx,this.image1.getHeight()+10, Graphics.TOP | Graphics.LEFT);
            g.drawImage(exit, xx, this.image1.getHeight()+40, Graphics.TOP | Graphics.LEFT);
        }
        else if(index==1)
        {
            g.drawImage(newgame, xx, this.image1.getHeight()+10, Graphics.TOP | Graphics.LEFT);
            g.drawImage(this._item_exit.GetFrame(),xx,this.image1.getHeight()+40, Graphics.TOP | Graphics.LEFT);
        }
        
        try {
            Thread.sleep(700);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        repaint();
    }

    class Animation {

        Image[] ArrayFrame;
        private int Nframe = 0;
        private int CurFrame = 0;

        public Animation() {
        }

        public Animation(Image[] hinh, int n) {
            this.Nframe = n;
            this.ArrayFrame = hinh;
        }

        public Image GetFrame() {
            CurFrame = (CurFrame + 1) % this.Nframe;
            return ArrayFrame[CurFrame];
        }
    }

    public void commandAction(Command cmnd, Displayable dsplbl) {
        if (cmnd == this._ok) {
            if(index==0)
            {
                this.app.ShowMHPlay();
            }
            else if(index==1)
            {
                this.app.ExitApp();
            }
        } 
    }
}
